/**
 * This file is part of Wireless TransFer.
 *
 *   Wireless TransFer is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *	
 *   Wireless TransFer is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Wireless TransFer.  If not, see <http://www.gnu.org/licenses/>.
 *
 *	 Copyright (C) 2011
 *   @author prebz
 */

package com.wirelesstransfer.activity.status;

import java.io.File;
import java.util.List;

import com.wirelesstransfer.R;
import com.wirelesstransfer.AsyncTasks.AsyncTaskElements;
import com.wirelesstransfer.client.ApacheClient;
import com.wirelesstransfer.client.ClientConnection;
import com.wirelesstransfer.connection.ITransferData;
import com.wirelesstransfer.server.ConnectedClients.ConnectedClient;
import com.wirelesstransfer.server.ConnectedClients.ConnectedClientsHandler;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;

public class ClientStatus extends ListActivity implements OnClickListener{

	private Resources res;

	private String bundleHolder;
	private String ip;
	private int action;
	private List<File> adapterList;

	private ArrayAdapter<File> arrayAdapter;

	private ApacheClient apacheClient;
	private ClientConnection clientConnection;
	
	private View pauseButton;
	private View cancelButton;

	private Intent intent;
	private Bundle bundle;
	
	private ITransferData files;
	
	private ConnectedClientsHandler connectedClients;
	private ConnectedClient client;
	
	public static final int DIALOG_CANCEL = 0;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.clientstatus);

		res = getResources();

		intent = this.getIntent();
		bundle = intent.getExtras();

		// Initiate sharedFiles button
		pauseButton = findViewById(R.id.btn_pause);
		pauseButton.setOnClickListener(this);
		cancelButton= findViewById(R.id.btn_cancel);
		cancelButton.setOnClickListener(this);

		bundleHolder = bundle.getString(res.getString(R.string.status_TITLE));
		ip = bundleHolder.split(":")[0];
		action = Integer.parseInt(bundleHolder.split(":")[1]);
		
		setTitle(ip);
		if(action == Status.SERVER){
			connectedClients = ConnectedClientsHandler.getInstance();
			for(ConnectedClient client : connectedClients.getConnectedClients()){
				Log.v("Status", client.getIP() + " " + ip);
				if(client.getIP().equals(ip)){
					this.client = client;
					break;
				}
			}
			
			files = client;
			
		}
		else {
			apacheClient = ApacheClient.getInstance();
			clientConnection = apacheClient.getConnection();
			files = clientConnection.getTransferData();
		}
		Log.v("Status",  "is null " + (files == null));

		
		
		adapterList = files.getFiles();
		
		arrayAdapter = new CustomClientArrayAdapter(this, adapterList, files);
		AsyncTaskElements.files = arrayAdapter;
		setListAdapter(arrayAdapter);

	}
	

	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btn_cancel:
			showDialog(DIALOG_CANCEL);
			break;
		case R.id.btn_pause:
			break;	
		}

	}
	
	@Override
	protected Dialog onCreateDialog (int id){
		Dialog dialog = null;

		switch (id) {
		case DIALOG_CANCEL:
			dialog = buildCancelDialog(res.getString(R.string.fileclient_dialog_cancel));
			break;
		default:
			dialog = null;
			break;
		}
		return dialog;

	}

	/**
	 * Creates text in a dialog with yes/no key
	 * @param action
	 * @return
	 */
	public AlertDialog buildCancelDialog(String acttion){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(acttion)
		.setCancelable(false)
		.setPositiveButton(res.getString(R.string.send_dialog_yes), new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				
				if(action == Status.SERVER){
					connectedClients.removeClient(client);
				}
				else{
					String sessionId = clientConnection.getSessionId();
					apacheClient.disconnect(sessionId);
				}
				
               ClientStatus.this.finish();
			}
		})
		.setNegativeButton(res.getString(R.string.send_dialog_no), new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});
		AlertDialog alert = builder.create();
		return alert;
	}
	
}
